﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using mojoPortal.Web;
using Resources;
using mojoPortal.Web.Framework;
using System.Collections;
using mojoPortal.Business;
using Telerik.Web.UI;
using ud_gSDMS.Code_Service;
using System.Collections.Generic;
using System.Web.UI;

namespace ud_gSDMS.contract
{
    public partial class Contract_Cat : mojoBasePage
    {
        private int pageId = -1;
        private int moduleId = -1;
        private SiteUser _siteUser;
        

        void Page_Load(object sender, EventArgs e)
        {
            if (SiteUtils.GetCurrentSiteUser()==null)
            {
                WebUtils.SetupRedirect(this, SiteRoot + "/AccessDenied.aspx");
            }
            else
            {
                _siteUser = SiteUtils.GetCurrentSiteUser();

                if (!Page.IsPostBack)
                {
                    PopulateLabels();
                    PopulateControls();
                }
            }
        }
        private void PopulateLabels()
        {

        }

        private void PopulateControls()
        {
            datContract_Date_from.SelectedDate = DateTime.Now.AddMonths(-3);
            datContract_Date_to.SelectedDate = DateTime.Now;
            datContract_Date_from.MaxDate = DateTime.Now;
            datContract_Date_to.MaxDate = DateTime.Now;
            LoadControl();
            LoadGrid();
        }
        private void LoadGrid()
        {
            var data = gSDMS_BL.ud_gSDMS_CONTRACT_Search(datContract_Date_from.SelectedDate == null ? null : datContract_Date_from.SelectedDate.Value.ToShortDateString(),
                                                         datContract_Date_to.SelectedDate == null ? null : datContract_Date_to.SelectedDate.Value.ToShortDateString(),
                                                         cboContract_Code.Text.Trim() == "" ? null : cboContract_Code.Text,
                                                         cboContract_Name.Text.Trim() == "" ? null : cboContract_Name.Text,
                                                         cboStatus.Text.Trim() == "" ? null : cboStatus.SelectedValue,
                                                         cboAccount_Manager.Text.Trim() == "" ? null : cboAccount_Manager.SelectedValue,
                                                         chkActive.Checked
                );

            if (ViewState["SortExpression"] != null)
            {
                var dv = data.DefaultView;
                dv.Sort = ViewState["SortExpression"].ToString();
                data = dv.Table;
            }
            grdContract.DataSource = data;
            grdContract.DataBind();
        }
        private void LoadControl()
        {
            cboContract_Code.DataValueField = "Contract_ID";
            cboContract_Code.DataTextField = "Contract_Code";
            cboContract_Code.DataSource = gSDMS_BL.ud_gSDMS_CONTRACT_Search(null, null, null, null, null, null, true);
            cboContract_Code.DataBind();

            cboContract_Name.DataValueField = "Contract_ID";
            cboContract_Name.DataTextField = "Contract_Name";
            cboContract_Name.DataSource = gSDMS_BL.ud_gSDMS_CONTRACT_Search(null, null, null, null, null, null, true);
            cboContract_Name.DataBind();

            var status = gSDMS_BL.ud_gSDMS_CAT_CONTRACT_STATUS_Lst();
            cboStatus.Items.Add(new RadComboBoxItem("", ""));
            foreach (var p in status)
            {
                cboStatus.Items.Add(new RadComboBoxItem(p.Name ,p.CAT_Contract_Status_ID.ToString()));
            }

            cboAccount_Manager.DataValueField = "UserID";
            cboAccount_Manager.DataTextField = "LoginName";
            cboAccount_Manager.DataSource = gSDMS_BL.mp_User_Search(null, null, null, null, null, null, "6");
            cboAccount_Manager.DataBind();

            if (gSDMS_BL.mp_User_IsRole(_siteUser.UserId, "12|1"))
                {
                    grdContract.Columns[7].Visible = true;
                }

            if (gSDMS_BL.mp_User_IsRole(_siteUser.UserId, "6|1"))
            {
                btnAddNew.Visible = true;
            }
        }

        #region event
        private void grdContract_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdContract.PageIndex = e.NewPageIndex;
            LoadGrid();
        }
        private void grdContract_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string id = e.CommandArgument.ToString();
            switch (e.CommandName)
            {
                case "delete" :
                     gSDMS_BL.ud_gSDMS_CONTRACT_Del(e.CommandArgument.ToString(), false);
                    break;
                case "editContract" :
                    WebUtils.SetupRedirect(this, SiteRoot + "/gsdmsmodule/contract/contract_edit.aspx?pageid=16&id=" + id);
                    break;
                case "acceptContract" :
                    WebUtils.SetupRedirect(this, SiteRoot + "/gsdmsmodule/contract/contract_accept.aspx?pageid=16&id=" + id);
                    break;
            }

        }
        void grdContract_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            LoadGrid();
        }
        void grdContract_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (ViewState["Direction"] == null) ViewState["Direction"] = " DESC";
            else if (ViewState["Direction"].ToString() == " ASC") ViewState["Direction"] = " DESC";
            else if (ViewState["Direction"].ToString() == " DESC") ViewState["Direction"] = " ASC";
            //grdContract.EditIndex = -1;
            string strSortExp = e.SortExpression + ViewState["Direction"].ToString();
            ViewState["SortExpression"] = strSortExp;
            
            LoadGrid();
        }

        void btnSearch_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            LoadGrid();
        }

        void btnAddNew_Click(object sender, EventArgs e)
        {
            WebUtils.SetupRedirect(this, SiteRoot + "/gsdmsmodule/contract/contract_edit.aspx?pageid=16");
        }

        void grdContract_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (_siteUser == null) return;

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string text = ((DataBoundLiteralControl)e.Row.Cells[4].Controls[0]).Text;

                if (text == _siteUser.LoginName || gSDMS_BL.mp_User_IsRole(_siteUser.UserId, "12|14|1"))
                {
                    e.Row.FindControl("btnEdit").Visible = true;
                    e.Row.FindControl("btnDelete").Visible = true;
                }
            }
        }

        void btnFinish_Click(object sender, EventArgs e)
        {
            WebUtils.SetupRedirect(this, SiteUtils.GetCurrentPageUrl());
        }

        #endregion
        #region OnInit

        protected override void OnPreInit(EventArgs e)
        {
            AllowSkinOverride = true;
            base.OnPreInit(e);
            Load += Page_Load;
        }

        protected override void OnInit(EventArgs e)
        {
            LoadParams();
            LoadPanels();
            base.OnInit(e);
            grdContract.PageIndexChanging += grdContract_PageIndexChanging;
            grdContract.RowCommand += grdContract_RowCommand;
            grdContract.RowDeleting += grdContract_RowDeleting;
            grdContract.Sorting += grdContract_Sorting;
            grdContract.RowDataBound += grdContract_RowDataBound;
            btnSearch.Click += btnSearch_Click;
            btnAddNew.Click += btnAddNew_Click;

            //btnFinish.Click += btnFinish_Click;
            SuppressPageMenu();
        }

        

        private void LoadParams()
        {
            pageId = WebUtils.ParseInt32FromQueryString("pageid", pageId);
            moduleId = WebUtils.ParseInt32FromQueryString("mid", true, moduleId);
        }

        private void LoadPanels()
        {
            Hashtable settings = ModuleSettings.GetModuleSettings(moduleId);
            //config = new ContactFormConfiguration(settings);
            bool showLeftColumnAdmin = ConfigHelper.GetBoolProperty("ShowLeftColumnAdmin", false);
            bool showRightColumnAdmin = ConfigHelper.GetBoolProperty("ShowRightColumnAdmin", false);
            bool showTopColumnAdmin = ConfigHelper.GetBoolProperty("ShowTopColumnAdmin", false);
            bool showBottomColumnAdmin = ConfigHelper.GetBoolProperty("ShowBottomColumnAdmin", false);
            int showCenterColumnAdmin = ConfigHelper.GetIntProperty("ShowCenterColumnAdmin", 0);
            LoadSideContent(showLeftColumnAdmin, showRightColumnAdmin, showTopColumnAdmin, showBottomColumnAdmin, showCenterColumnAdmin);
        }
        #endregion
    }
}